package eu.connect.im;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.ServerSocket;
import java.net.Socket;

public class RequestListenerThread extends Thread
{
	private final IMCONNECTor connector;
    private final ServerSocket serversocket;
    
    public RequestListenerThread(IMCONNECTor connector, int port) throws IOException
    {
    	this.connector = connector;
        this.serversocket = new ServerSocket(port);
    }
    
    public void run()
    {
        System.out.println("Listening on port " + this.serversocket.getLocalPort());
        while (!Thread.interrupted())
        {
            try
            {
                // Set up HTTP connection
                Socket socket = this.serversocket.accept();
                //System.out.println("Incoming connection from " + socket.getInetAddress());
                Thread t = new RequestHandleThread(this.connector, socket);
                t.setDaemon(true);
                t.start();
                
            } catch (InterruptedIOException ex) {
                break;
            } catch (IOException e) {
                System.err.println("I/O error initialising connection thread: " 
                        + e.getMessage());
                break;
            }
        }
    }
}
